package core.controls;
import ibm.widgets.WButton;
import ibm.widgets.ancestors.IWidget;
import ibm.widgets.ancestors.Widget;

import com.rational.test.ft.domain.java.swt.WidgetProxy;
import com.rational.test.ft.object.interfaces.TestObject;
import com.rational.test.util.regex.Regex;

import core.exceptions.ResultFailureException;
import core.testcases.TestObj;
/**
 * Description   : Functional Test Script
 * @author angel.tsvetkov
 */
public class Button extends Control<WButton>
{
	public Button(String propertyValue, String propertyName, String objClass,
			TestObject parent, String descriptiveName) {
		super(propertyValue, propertyName, objClass, parent, descriptiveName);
		this.obj = null;
	}
	
	public Button(TestObject object, String descriptiveName) {
		super(object, descriptiveName);
		this.obj = null;
	}

	public boolean clickButton()
	{
        boolean result = false;
        String taskReport = null;

        try
        {
            taskReport = " \n Press " + this.getDescriptiveName();
            this.click();
            result = true;
        }
        catch(ResultFailureException e)
        {
        	setErrorComment("Problem appear in pressing of button with prop name: " + this.PropName() + " and prop value: " + this.PropValue(),e);
        }
        
        setTaskResult("Task: Press " + this.getDescriptiveName() , result, taskReport);

        return result;
	}

	/**
	 * Description : Click button.
	 * 
	 * @param fieldPropValue - property value which is used for button determination
	 * 
	 * @param fieldPropName - property name which is used for button determination
	 * 
	 * @param buttonType - button type
	 * 
	 * @param parent - area in which the button belongs.
	 */
	public boolean click()
			throws ResultFailureException {
		// Initialize variables
		boolean result = false;
		WButton buttonToClick = null;

		// Perform click button
		try {
			// Allocate button by regular expression
			buttonToClick = this.get();

			// Click it
			buttonToClick.click();

			result = true;
		} catch (Exception e) {
			throw new ResultFailureException(
					"Unexpected assertion on pressing " + this.getDescriptiveName());
		}
		finally
		{
			// Unregister object
			buttonToClick.unregister();
		}
		// TODO: Check whether this sleep is needed
		TimeWait(3);

		return result;
	}
	
	public static boolean verifyButtonName(TestObject button, String expectedName)
	{
        boolean result = false;
        String taskReport = null;
        String stretchButtonName = null;

        try
        {
        	taskReport = " \n Verify " + button.getDescriptiveName() +" name.";
	        stretchButtonName = (String) button.getProperty(".value");
        }catch(Exception ex)
        {
        	setErrorComment("Problem appear in verification the name of button : " + button.getDescriptiveName(),ex);
        }
            
        result = expectedName.equals(stretchButtonName);

        setTaskResult("Task: Verify " + button.getDescriptiveName() + " name.", result, taskReport);

        return result;
	}
	
	public boolean verifyButtonName(String expectedName)
	{
        boolean result = false;
        String taskReport = null;
        String stretchButtonName = null;

        try
        {
        	taskReport = " \n Verify " + this.getDescriptiveName() +" name.";
	        stretchButtonName = (String) this.get().getProperty(".value");
        }catch(Exception ex)
        {
        	setErrorComment("Problem appear in verification the name of button with prop name: " + this.PropName() + " and prop value: " + this.PropValue(),ex);
        }
            
        result = expectedName.equals(stretchButtonName);

        setTaskResult("Task: Verify " + this.getDescriptiveName() + " name.", result, taskReport);

        return result;
	}

	@Override
	public WButton get() {
		if(isCreatedFromTestObject())
		{
			this.obj = new WButton(testObject);
		}
		else
		{
			if(this.isCognosControl())
			{
				TestObject parent = this.getParent();
				this.obj = new WButton(parent.getChildren()[0]);
			}
			else
			{
				Regex toSearch = new Regex(this.PropValue(), Regex.MATCH_MULTILINE);
				this.obj = new WButton(toSearch,this.PropName(),this.getObjClass(),this.getParent());
			}
		}
		return obj;
	}

	@Override
	public boolean isEnabled() {
		return this.get().isEnabled();
	}

	@Override
	public boolean isExist() {
		return this.get().ensureObjectIsVisible();
	}

}

